﻿namespace QLPhongKham.Access.Duoc
{
    using ConfigDatabase.Class;
    using Npgsql;
    using QLPhongKham.Access;
    using QLPhongKham.BaseClass.Duoc;
    using System;
    using System.Data;

    public class DDuocPham
    {
        private ConnectDatabase _condata;
        private NpgsqlConnection _conn;
        private General _xuli;

        public DDuocPham()
        {
            this._condata = new ConnectDatabase();
            this._xuli = new General(this._condata);
        }

        public DDuocPham(ConnectDatabase condata)
        {
            this._condata = condata;
            this._xuli = new General(this._condata);
        }

        public DataTable f_loaddata_dmdonggoi(int iddp)
        {
            DataTable table = new DataTable();
            table = this.f_loaddata_dmquydoi(0L, iddp);
            DataRow row = table.NewRow();
            DataTable table2 = this._xuli.f_Get_dulieu(string.Concat(new object[] { 
                "select 0 as id,", dtoDDMDuocPham.columnDVT, " as ten,1 as soluong,", dtoDDMDuocPham.columnID, " as iddp,", dtoDDMDuocPham.columnTEN, " as tendp,", dtoDDMDuocPham.columnMA, " as madp,", dtoDDMDuocPham.columnDVT, " as dvt from ", this._xuli.pSchema, ".", dtoDDMDuocPham.tableName, " where ", dtoDDMDuocPham.columnID, 
                "=", iddp
             })).Tables[0];
            row["id"] = 0;
            row["ten"] = table2.Rows[0]["ten"].ToString();
            row["soluong"] = 1;
            row["iddp"] = iddp;
            row["tendp"] = table2.Rows[0]["tendp"].ToString();
            row["madp"] = table2.Rows[0]["madp"].ToString();
            row["dvt"] = table2.Rows[0]["dvt"].ToString();
            table.Rows.InsertAt(row, 0);
            return table;
        }

        public DataTable f_loaddata_dmduocpham()
        {
            DataTable table = new DataTable();
            string schema = this._condata.Schema;
            return this._xuli.f_Get_dulieu("select a." + dtoDDMDuocPham.columnID + " as id,a." + dtoDDMDuocPham.columnMA + " as ma,a." + dtoDDMDuocPham.columnTEN + " as ten,a." + dtoDDMDuocPham.columnDVT + " as dvt,a." + dtoDDMDuocPham.columnTENHOATCHAT + " as tenhoatchat,a." + dtoDDMDuocPham.columnHAMLUONG + " as hamluong,a." + dtoDDMDuocPham.columnDONGIA + " as dongia,a." + dtoDDMDuocPham.columnSTT + " as stt,a." + dtoDDMDuocPham.columnGHICHU + ",a." + dtoDDMDuocPham.columnIDLOAI + ",a." + dtoDDMDuocPham.columnIDNHOM + ",a." + dtoDDMDuocPham.columnIDNHASX + ",a3." + dtoDDMNhaSanXuat.columnTEN + " as tennhasx,a1." + dtoDDMNhomDuocPham.columnTEN + " as tennhom,a2." + dtoDDMLoaiDuocPham.columnTEN + " as tenloai from " + schema + "." + dtoDDMDuocPham.tableName + " a left join " + schema + "." + dtoDDMNhomDuocPham.tableName + " a1 on a." + dtoDDMDuocPham.columnIDNHOM + "=a1." + dtoDDMNhomDuocPham.columnID + " left join " + schema + "." + dtoDDMLoaiDuocPham.tableName + " a2 on a." + dtoDDMDuocPham.columnIDLOAI + "=a2." + dtoDDMLoaiDuocPham.columnID + " left join " + schema + "." + dtoDDMNhaSanXuat.tableName + " a3 on a." + dtoDDMDuocPham.columnIDNHASX + "=a3." + dtoDDMNhaSanXuat.columnID + " order by a." + dtoDDMDuocPham.columnNGAYCN + " desc," + dtoDDMDuocPham.columnTEN).Tables[0];
        }

        public DataTable f_loaddata_dmquydoi(long id)
        {
            return this.f_loaddata_dmquydoi(id, 0);
        }

        public DataTable f_loaddata_dmquydoi(long id, int iddp)
        {
            DataTable table = new DataTable();
            string str = this._condata.Schema + ".";
            string sql = "select a." + dtoDDMQuyDoi.columnID + " as id,a." + dtoDDMQuyDoi.columnTEN + " as ten,a." + dtoDDMQuyDoi.columnSOLUONG + " as soluong,a." + dtoDDMQuyDoi.columnIDDUOCPHAM + " as iddp,b." + dtoDDMDuocPham.columnTEN + " as tendp,b." + dtoDDMDuocPham.columnMA + " as madp,b." + dtoDDMDuocPham.columnDVT + " as dvt from " + str + dtoDDMQuyDoi.tableName + " a inner join " + str + dtoDDMDuocPham.tableName + " b on a." + dtoDDMQuyDoi.columnIDDUOCPHAM + "=b." + dtoDDMDuocPham.columnID + " where 1=1 " + ((id != 0L) ? string.Concat(new object[] { " and a.", dtoDDMQuyDoi.columnID, "=", id }) : "") + ((iddp != 0) ? string.Concat(new object[] { " and a.", dtoDDMQuyDoi.columnIDDUOCPHAM, "=", iddp }) : "") + " order by a." + dtoDDMQuyDoi.columnIDDUOCPHAM + " desc,a." + dtoDDMQuyDoi.columnSOLUONG + " asc";
            return this._xuli.f_Get_dulieu(sql).Tables[0];
        }
    }
}

